package tas.java;

import java.sql.Date;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public final class Convierte {

    public static Integer aInteger(Object obj) {
        Integer x = null;

        if (obj != null) {
            if (obj instanceof String) {
                try {
                    x = Integer.valueOf(obj.toString());
                } catch (NumberFormatException ex) {
                }

            } else if (obj instanceof Double) {
                x = ((Double) obj).intValue();

            } else if (obj instanceof Integer) {
                x = (Integer) obj;
            }
        }

        return x;
    }

    public static Long aLong(Object obj) {
        Long x = null;

        if (obj != null) {
            if (obj instanceof String) {
                try {
                    x = Long.valueOf(obj.toString());
                } catch (NumberFormatException ex) {
                }

            } else if (obj instanceof Double) {
                x = ((Double) obj).longValue();

            } else if (obj instanceof Integer) {
                x = ((Integer) obj).longValue();

            } else if (obj instanceof Long) {
                x = (Long) obj;
            }
        }

        return x;
    }

    public static Double aDouble(Object obj) {
        Double x = null;

        if (obj != null) {
            if (obj instanceof String) {
                try {
                    x = Double.valueOf(obj.toString());
                } catch (NumberFormatException ex) {
                }

            } else if (obj instanceof Integer) {
                x = 1D * ((Integer) obj);

            } else if (obj instanceof Double) {
                x = (Double) obj;
            }
        }

        return x;
    }

    public static String aString(Object obj) {
        String s = null;

        if (obj != null) {
            if (obj instanceof Integer) {
                s = String.valueOf(obj);

            } else if (obj instanceof Long) {
                s = String.valueOf(obj);

            } else if (obj instanceof Double) {
                s = String.valueOf(obj);

            } else if (obj instanceof Date) {
                SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
                java.util.Date utilDate = new java.util.Date(((Date) obj).getTime());
                s = sdf.format(utilDate);

            } else if (obj instanceof Timestamp) {
                SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
                java.util.Date utilDate = new java.util.Date(((Timestamp) obj).getTime());
                s = sdf.format(utilDate);

            } else if (obj instanceof String) {
                s = obj.toString();
            }
        }

        return s;
    }

    // llega String "dd/MM/yyyy" o Date y retorna Date (yyyy-MM-dd)
    public static Date aDate(Object obj) {
        Date fecha = null;

        if (obj != null) {
            if (obj instanceof String) {
                SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

                try {
                    java.util.Date utilDate = sdf.parse(obj.toString());
                    fecha = new Date(utilDate.getTime());

                } catch (ParseException ex) {
                }

            } else if (obj instanceof Date) {
                fecha = (Date) obj;
            }
        }

        return fecha;
    }

    // llega String "dd/MM/yyyy" y retorna yyyy-MM-dd HH:mm:ss
    public static Timestamp aTimestamp(Object obj) {
        Timestamp diahora = null;

        if (obj != null) {
            if (obj instanceof String) {
                SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");

                try {
                    java.util.Date utilDate = sdf.parse(obj.toString());
                    diahora = new Timestamp(utilDate.getTime());

                } catch (ParseException ex) {
                }

            } else if (obj instanceof Timestamp) {
                diahora = (Timestamp) obj;
            }
        }

        return diahora;
    }
}
